package ExerChapter15Collection_;

// 要求姓名生日一样则不能添加在HashSet中

import java.util.HashSet;

public class HashSet_ {
    public static void main(String[] args) {
        HashSet set = new HashSet();
        Employee e1 = new Employee("A", 1000, new MyDate(1990, 1, 1));
        Employee e2 = new Employee("B", 2000, new MyDate(1991, 2, 2));
        Employee e3 = new Employee("A", 3000, new MyDate(1990, 1, 1));
        set.add(e1);
        set.add(e2);
        set.add(e3);

        System.out.println(set);
    }

}

class Employee{
    private String name;
    private double sal;
    private MyDate birthday;

    public Employee(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((birthday == null) ? 0 : birthday.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Employee other = (Employee) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (birthday == null) {
            if (other.birthday != null)
                return false;
        } else if (!birthday.equals(other.birthday))
            return false;
        return true;
    }

    
    @Override
    public String toString() {
        return "Employee [name=" + name + ", sal=" + sal + ", birthday=" + birthday + "]";
    }

    
}

class MyDate{
    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + year;
        result = prime * result + month;
        result = prime * result + day;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        MyDate other = (MyDate) obj;
        if (year != other.year)
            return false;
        if (month != other.month)
            return false;
        if (day != other.day)
            return false;
        return true;
    }

    

    
}